package bdqn.com.cui.serviet;

import bdqn.com.cui.pojo.Record;
import bdqn.com.cui.service.RecordService;
import bdqn.com.cui.service.impl.RecordserviceImpl;
import bdqn.com.cui.utl.LayData;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "recordservlet",urlPatterns = "/record")
public class Recordservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");//设置页面属性为  UTF-8
        req.setCharacterEncoding("UTF-8");
        String opr=req.getParameter("opr");
        RecordService recordService=new RecordserviceImpl();
        if(opr.equals("select")) { //全查
            int current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            List<Record> list = recordService.getsenect((current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
            int count = recordService.getcount();
            LayData layData = new LayData();
            layData.setCode(0);
            layData.setMsg("");
            layData.setCount(count);
            layData.setData(list);
            String shop_name = req.getParameter("p_name");
            if (shop_name!=null&&shop_name!=""){
                List<Record> idset = recordService.getidserent(shop_name);
                layData.setData(idset);
            }else if(shop_name==null||shop_name==""){
                List<Record> list1= recordService.getsenect((current-1)*Integer.parseInt(limit), Integer.parseInt(limit));
                layData.setData(list1);
            }
            String jsonstring= JSONObject.toJSONString(layData);
            resp.getWriter().write(jsonstring);
        }else if (opr.equals("delete")) {//删除
            String id = req.getParameter("id");
            int det = recordService.delete(id);
            boolean f = false;
            if (det > 0) {
                f = true;
            }
            resp.getWriter().write(String.valueOf(f));
        }else if (opr.equals("update")) {
            String id = req.getParameter("id");
            String i = req.getParameter("i");
            int typeupdate = recordService.Typeupdate(i, id);
            String s = JSONObject.toJSONString(typeupdate);
            resp.getWriter().write(s);
        }
//        }else if (opr.equals("add")){
//            String name=req.getParameter("name");
//            String leixing=req.getParameter("leixing");
//            String jiage=req.getParameter("jiage");
//            String jieshao=req.getParameter("jieshao");
//            String kucun=req.getParameter("kucun");
//            String zhanghaome=req.getParameter("zhanghao");
//            String filename=req.getParameter("filename");
//            int i= recordService.add(new Commodity(name,leixing,Integer.parseInt(jiage),filename,jieshao,Integer.parseInt(kucun),zhanghaome,0,2));
//            String s=JSONObject.toJSONString(i);
//            resp.getWriter().write(s);
//        }else if (opr.equals("fenleiselect")){
//            List<Commodity> list =commodityservice.fenleiselect();
//            req.getSession().setAttribute("list",list);
//            resp.sendRedirect("shangpin_add.jsp");
//        }
    }
}
